<template>
  <div class="right">
    <downloadIcon></downloadIcon>
    <div class="voice_icon">
      <voiceIcon @click="showVolumeHandler"></voiceIcon>
      <div v-if="showVolume" class="voice_progress">
        <span class="voice_progress_num">{{ volumeCpd }}%</span>
        <div class="progress">
          <ltSlider v-model="volume" :max="100" @change="changeVolume"></ltSlider>
        </div>
      </div>
    </div>
    <listIcon @click="songListHandler"></listIcon>

    <ltDialog v-model="isShow" :is-mask="true" :is-header="false">
      <div v-if="playStore.playList.length > 0" class="song_list">
        <songList
          v-for="item in playStore.playList"
          :key="item.id"
          :item="item"
          @click="changeSongHan(item)"
        ></songList>
      </div>
    </ltDialog>
  </div>
</template>

<script setup lang="ts">
import { computed, onMounted, onUnmounted, ref } from 'vue'

import listIcon from '../icon/list-icon.vue'
import voiceIcon from '../icon/voice-icon.vue'
import downloadIcon from '../icon/download-icon.vue'
import ltDialog from '@renderer/base-ui/lt-dialog/index.vue'
import songList from './song-list.vue'
import ltSlider from '@renderer/base-ui/lt-slider/index.vue'

import usePlay from '@renderer/pinia/modules/play'

const showVolume = ref(false) // 是否展示音量
const playStore = usePlay()

const volume = ref<number>(playStore.volume * 100) // 音量

const volumeCpd = computed(() => {
  // 计算音量的变化
  return parseInt(volume.value as any)
})

// 显示音量调整
const showVolumeHandler = (e: MouseEvent) => {
  e.stopPropagation()
  showVolume.value = true
}
const hideVolumeHandler = (e: MouseEvent) => {
  // 如果点击的元素包含lt_slider_progress，则不隐藏音量调整
  if ((e.target as any).classList.contains('lt_slider_bg')) {
    return
  }
  if ((e.target as any).classList.contains('lt_slider_progress')) {
    return
  }
  if ((e.target as any).classList.contains('lt_slider_thumb')) {
    return
  }
  if ((e.target as any).classList.contains('voice_progress')) {
    return
  }
  showVolume.value = false
}
onMounted(() => {
  document.addEventListener('click', hideVolumeHandler)
})
onUnmounted(() => {
  document.removeEventListener('click', hideVolumeHandler)
})

// 改变音量
const changeVolume = (val: number) => {
  const newVolume = Math.ceil(val) / 100
  playStore.setVolume(newVolume)
}

// 展示播放歌曲列表的处理
const isShow = ref(false)
const songListHandler = () => {
  isShow.value = true
}

// 点击切换歌曲
const changeSongHan = (item: any) => {
  playStore.setCurrentAudio(item)
  playStore.setPlayList(item)
}
</script>

<style scoped lang="scss">
.right {
  width: 200px;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  /* background-color: blue; */

  .voice_icon {
    position: relative;
    .voice_progress {
      height: 30px;
      width: 130px;
      position: absolute;
      top: -85px;
      left: -44px;
      display: flex;
      align-items: center;
      border-radius: 5px;
      transform: rotate(-90deg);
      padding: 0 5px;
      background-color: #171f34cc;

      .voice_progress_num {
        font-size: 12px;
        position: absolute;
        right: 0;
        user-select: none;
        transform: rotate(90deg);
      }
      .progress {
        width: 90px;
        background-color: yellow;
      }
    }
  }
}
</style>
